﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using KMFrame.Core;
using NBearLite;
using LGCourt.Logic.NBL;
using LGCourt.Logic.Common;
using LGCourt.Logic.NBL.DBEntities;

namespace LGCourt.Logic.BLL
{
    public class Lawyer : LGCourtLogic
    {
        /// <summary>
        /// 案件信息
        /// </summary>
        /// <param name="param"></param>
        /// <returns></returns>
        public CaseALLInfo GetCaseALLInfo(UserCondition param)
        {
            ExpressionClip[] laxxColumns =
            new ExpressionClip[] { 
                LGCourtColumns.vLawyerLAXX.AH,
                LGCourtColumns.vLawyerLAXX.UserID,
                LGCourtColumns.vLawyerLAXX.UserName,                
            };
            vLawyerLAXX vlaxx = NBLData.Select(LGCourtColumns.vLawyerLAXX, laxxColumns)
               .Where(LGCourtColumns.vLawyerLAXX.AH == param.AH && LGCourtColumns.vLawyerLAXX.UserID == param.UserID)
               .ToSingleObject<vLawyerLAXX>();
            if (vlaxx == null)
                return null;

            CaseALLInfo result = new CaseALLInfo();
            result = NBLData.Select(LGCourtColumns.pLAXX)
               .Where(LGCourtColumns.pLAXX.AH == param.AH)
               .ToSingleObject<CaseALLInfo>();

            if (result != null)
            {
                //当事人
                result.DSRXXList = NBLData.Select(LGCourtColumns.pDSRXX)
               .Where(LGCourtColumns.pDSRXX.AH == param.AH)
               .OrderBy(LGCourtColumns.pDSRXX.LX.Desc)
               .ToList<pDSRXX>();

                ExpressionClip[] wsqsColumns =
            new ExpressionClip[] { 
                LGCourtColumns.pWSQS.WSQSID,
                LGCourtColumns.pWSQS.AH,
                LGCourtColumns.pWSQS.WSXH,
                LGCourtColumns.pWSQS.WSMC,
                LGCourtColumns.pWSQS.SFQS,
                LGCourtColumns.pWSQS.QSRQ,
                LGCourtColumns.pWSQS.SDRQ
            };
                //文书签收
                result.WSQSList = NBLData.Select(LGCourtColumns.pWSQS, wsqsColumns)
               .Where(LGCourtColumns.pWSQS.AH == param.AH)
               .OrderBy(LGCourtColumns.pWSQS.SDRQ.Desc)
               .ToList<pWSQS>();

                ExpressionClip[] zjjhColumns =
           new ExpressionClip[] { 
                LGCourtColumns.pZJJHB.ZZJHID,
                LGCourtColumns.pZJJHB.AH,
                LGCourtColumns.pZJJHB.ZJMC,
                LGCourtColumns.pZJJHB.TJRQ,
                LGCourtColumns.pZJJHB.SFSH,
                LGCourtColumns.pZJJHB.TJRXM,
            };
                //证据交换
                result.ZJJHList = NBLData.Select(LGCourtColumns.pZJJHB, zjjhColumns)
               .Where(LGCourtColumns.pZJJHB.AH == param.AH && (LGCourtColumns.pZJJHB.SFSH == 1 || (LGCourtColumns.pZJJHB.SFSH == 0 && LGCourtColumns.pZJJHB.TJRXM == vlaxx.UserName)))
               .OrderBy(LGCourtColumns.pZJJHB.TJRQ.Desc)
               .ToList<pZJJHB>();

                //预约法官
                result.YYFGList = NBLData.Select(LGCourtColumns.pYYFG)
               .Where(LGCourtColumns.pYYFG.AH == param.AH && LGCourtColumns.pYYFG.YYR == vlaxx.UserName)
               .OrderBy(LGCourtColumns.pYYFG.SQRQ.Desc)
               .ToList<pYYFG>();

                //判后答疑
                result.PHDYList = NBLData.Select(LGCourtColumns.pPHDY)
               .Where(LGCourtColumns.pPHDY.AH == param.AH)
               .OrderBy(LGCourtColumns.pPHDY.CJRQ.Desc)
               .ToList<pPHDY>();

                //执行信息
                result.ZZFSList = NBLData.Select(LGCourtColumns.pZZFS)
               .Where(LGCourtColumns.pZZFS.AH == param.AH)
               .OrderBy(LGCourtColumns.pZZFS.SQSJ.Desc)
               .ToList<pZZFS>();
            }

            return result;
        }

        public IEnumerable<WSTZItem> GetWSTZTop(int userid)
        {
            UserCondition condition = new UserCondition();
            condition.PageSize = 5;
            condition.UserID = userid;
            return GetWSTZ(condition).Select.ToList<WSTZItem>(); ;
        }

        private ConditionCollection GetWSTZ(UserCondition condition)
        {
            ConditionCollection cdtColl = new ConditionCollection();
            ExpressionClip[] selectColumns =
            new ExpressionClip[] { 
                LGCourtColumns.vLawyerWSQS.WSQSID,
                LGCourtColumns.vLawyerWSQS.AH,
                LGCourtColumns.vLawyerWSQS.WSMC,
                LGCourtColumns.vLawyerWSQS.SFQS,
                LGCourtColumns.vLawyerWSQS.QSRQ,
                LGCourtColumns.vLawyerWSQS.SDRQ
            };
            cdtColl.SelectColumns = selectColumns;

            WhereClip where = LGCourtColumns.vLawyerWSQS.UserID == condition.UserID;
            cdtColl.Where = where;

            cdtColl.Select = NBLData.Select(LGCourtColumns.vLawyerWSQS, selectColumns)
                .Where(where)
                .SetSelectRange(condition.PageSize, condition.PageSize * (condition.PageIndex - 1), LGCourtColumns.vLawyerWSQS.WSQSID)
                .OrderBy(LGCourtColumns.vLawyerWSQS.WSQSID.Desc);

            return cdtColl;
        }

        public PageResult GetWSTZPage(UserCondition condition)
        {
            PageResult result = new PageResult();

            result.ResultList = GetWSTZ(condition).Select.ToList<WSTZItem>();
            int total = -1;
            if (condition.TotalCount == -1)
            {
                total = GetTotalRecord(LGCourtColumns.vLawyerWSQS, LGCourtColumns.vLawyerWSQS.WSQSID, GetWSTZ(condition).Where);
            }
            result.PageIndex = condition.PageIndex;
            result.PageSize = condition.PageSize;
            result.TotalCount = total;
            return result;
        }

        private ConditionCollection GetMyCase(UserCondition condition)
        {
            ConditionCollection cdtColl = new ConditionCollection();
            ExpressionClip[] selectColumns =
            new ExpressionClip[] { 
                LGCourtColumns.vLawyerLAXX.CaseInfoID,
                LGCourtColumns.vLawyerLAXX.AH,
                LGCourtColumns.vLawyerLAXX.CBR,
                LGCourtColumns.vLawyerLAXX.SJY,
                LGCourtColumns.vLawyerLAXX.LXDH
            };
            cdtColl.SelectColumns = selectColumns;

            WhereClip where = LGCourtColumns.vLawyerLAXX.UserID == condition.UserID;
            cdtColl.Where = where;

            cdtColl.Select = NBLData.Select(LGCourtColumns.vLawyerLAXX, selectColumns)
                .Where(where)
                .SetSelectRange(condition.PageSize, condition.PageSize * (condition.PageIndex - 1), LGCourtColumns.vLawyerLAXX.CaseInfoID)
                .OrderBy(LGCourtColumns.vLawyerLAXX.CaseInfoID.Desc);

            return cdtColl;
        }

        public IEnumerable<MyCaseItem> GetMyCaseTop(int userid)
        {
            UserCondition condition = new UserCondition();
            condition.PageSize = 5;
            condition.UserID = userid;
            return GetMyCase(condition).Select.ToList<MyCaseItem>();
        }

        public PageResult GetMyCasePage(UserCondition condition)
        {
            PageResult result = new PageResult();

            result.ResultList = GetMyCase(condition).Select.ToList<MyCaseItem>();
            int total = -1;
            if (condition.TotalCount == -1)
            {
                total = GetTotalRecord(LGCourtColumns.vLawyerLAXX, LGCourtColumns.vLawyerLAXX.CaseInfoID, GetMyCase(condition).Where);
            }
            result.PageIndex = condition.PageIndex;
            result.PageSize = condition.PageSize;
            result.TotalCount = total;
            return result;
        }

        private ConditionCollection GetMyReback(UserCondition condition)
        {
            ConditionCollection cdtColl = new ConditionCollection();
            ExpressionClip[] selectColumns =
            new ExpressionClip[] { 
                LGCourtColumns.vLawyerMyReback.AH,
                LGCourtColumns.vLawyerMyReback.flag,
                LGCourtColumns.vLawyerMyReback.remark,
                LGCourtColumns.vLawyerMyReback.addtime,
                LGCourtColumns.vLawyerMyReback.status
            };
            cdtColl.SelectColumns = selectColumns;

            WhereClip where = LGCourtColumns.vLawyerMyReback.UserID == condition.UserID;
            cdtColl.Where = where;

            cdtColl.Select = NBLData.Select(LGCourtColumns.vLawyerMyReback, selectColumns)
                .Where(where)
                .SetSelectRange(condition.PageSize, condition.PageSize * (condition.PageIndex - 1), LGCourtColumns.vLawyerMyReback.addtime)
                .OrderBy(LGCourtColumns.vLawyerMyReback.addtime.Desc);

            return cdtColl;
        }

        public IEnumerable<MyRebackItem> GetMyRebackTop(int userid)
        {
            UserCondition condition = new UserCondition();
            condition.PageSize = 5;
            condition.UserID = userid;
            return GetMyReback(condition).Select.ToList<MyRebackItem>();
        }

        public PageResult GetMyRebackPage(UserCondition condition)
        {
            PageResult result = new PageResult();

            result.ResultList = GetMyCase(condition).Select.ToList<MyRebackItem>();
            int total = -1;
            if (condition.TotalCount == -1)
            {
                total = GetTotalRecord(LGCourtColumns.vLawyerMyReback, LGCourtColumns.vLawyerMyReback.addtime, GetMyReback(condition).Where);
            }
            result.PageIndex = condition.PageIndex;
            result.PageSize = condition.PageSize;
            result.TotalCount = total;
            return result;
        }

        //新增证据
        public bool AddZJJH(pZJJHB param)
        {
            InsertSqlSection insert = NBLData.Insert(LGCourtColumns.pZJJHB)
                            .AddColumn(LGCourtColumns.pZJJHB.AH, param.AH)
                            .AddColumn(LGCourtColumns.pZJJHB.ZJMC, param.ZJMC)
                            .AddColumn(LGCourtColumns.pZJJHB.ZJLX, param.ZJLX)
                            .AddColumn(LGCourtColumns.pZJJHB.ZJWJM, param.ZJWJM)
                            .AddColumn(LGCourtColumns.pZJJHB.TJRXM, param.TJRXM)
                            .AddColumn(LGCourtColumns.pZJJHB.ZJNR, param.ZJNR)
                            .AddColumn(LGCourtColumns.pZJJHB.TJRQ, DateTime.Now)
                            .AddColumn(LGCourtColumns.pZJJHB.UserID, param.UserID)
                            .AddColumn(LGCourtColumns.pZJJHB.SFSH, 0);

            return insert.Execute() > 0;
        }
        /// <summary>
        /// 新增预约法官信息
        /// </summary>
        /// <param name="param"></param>
        /// <returns></returns>
        public bool AddYYFG(pYYFG param)
        {
            InsertSqlSection insert = NBLData.Insert(LGCourtColumns.pYYFG)
                .AddColumn(LGCourtColumns.pYYFG.AH, param.AH)
                .AddColumn(LGCourtColumns.pYYFG.FG, param.FG)
                .AddColumn(LGCourtColumns.pYYFG.SQRQ, DateTime.Now)
                .AddColumn(LGCourtColumns.pYYFG.YYR, param.YYR)
                .AddColumn(LGCourtColumns.pYYFG.YYSJ, param.YYSJ)
                .AddColumn(LGCourtColumns.pYYFG.BZ, param.BZ)
                .AddColumn(LGCourtColumns.pYYFG.UserID, param.UserID)
                .AddColumn(LGCourtColumns.pYYFG.YYZT, 0);

            return insert.Execute() > 0;
        }

        public bool CancelYYFG(int yyfgid, int userid)
        {
            bool restul = false;
            pYYFG yyfg = NBLData.Select(LGCourtColumns.pYYFG)
                .Where(LGCourtColumns.pYYFG.YYFGID == yyfgid)
                .ToSingleObject<pYYFG>();
            if (yyfg != null)
            {
                vLawyerLAXX laxx = NBLData.Select(LGCourtColumns.vLawyerLAXX)
                    .Where(LGCourtColumns.vLawyerLAXX.AH == yyfg.AH && LGCourtColumns.vLawyerLAXX.UserID == userid)
                    .ToSingleObject<vLawyerLAXX>();

                if (laxx != null)
                {
                    int i = NBLData.Delete(LGCourtColumns.pYYFG).Where(LGCourtColumns.pYYFG.YYFGID == yyfgid).Execute();
                    if (i > 0) restul = true;
                }
            }
            return restul;
        }

        /// <summary>
        /// 新增判后答疑
        /// </summary>
        /// <param name="param"></param>
        /// <returns></returns>
        public bool AddPHDY(pPHDY param)
        {
            pSSDLR dsrxx = NBLData
               .Select(LGCourtColumns.pSSDLR)
               .Where(LGCourtColumns.pSSDLR.AH == param.AH && LGCourtColumns.pSSDLR.XM == param.WDR)
               .ToSingleObject<pSSDLR>();

            InsertSqlSection insert = NBLData.Insert(LGCourtColumns.pPHDY)
                 .AddColumn(LGCourtColumns.pPHDY.AH, param.AH)
                 .AddColumn(LGCourtColumns.pPHDY.WDR, param.WDR)
                 .AddColumn(LGCourtColumns.pPHDY.CJRQ, DateTime.Now)
                 .AddColumn(LGCourtColumns.pPHDY.UserID, param.UserID)
                 .AddColumn(LGCourtColumns.pPHDY.NR, param.NR)
                 .AddColumn(LGCourtColumns.pPHDY.SF, "律师");

            return insert.Execute() > 0;
        }
    }
}
